package com.lgl.scsheader.system.interceptor.shiro;

import com.lgl.scsapi.system.model.SysUser;
import com.lgl.scsapi.util.utils.Constants;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

/**
 * 权限验证
 */
public class ShiroRealm extends AuthorizingRealm {
	/*
	 * 登录信息和用户验证信息验证(non-Javadoc)
	 * @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken userToken = (UsernamePasswordToken)token;
        String hashPassword = new SimpleHash(Constants.ALGORITHM_NAME,userToken.getPassword(), Constants.SHIRO_SALT,Constants.HASH_ITERATIONS).toHex();
        if(userToken == null) {
            throw new UnknownAccountException("账号或密码不正确");
        }
        SysUser tokenUser = new SysUser(userToken.getUsername(), hashPassword);
        return new SimpleAuthenticationInfo(tokenUser, hashPassword, ByteSource.Util.bytes(Constants.SHIRO_SALT),getName());
	}
	
	/*
	 * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用,负责在应用程序中决定用户的访问控制的方法(non-Javadoc)
	 * @see org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
		return null;
	}

}